package org.webArchi.common.web.action.management;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.PrincipalAware;
import org.apache.struts2.interceptor.PrincipalProxy;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webArchi.common.api.domain.User;
import org.webArchi.common.api.service.UserManagmentService;
import org.webArchi.common.exception.SriTelRuntimeException;

import javax.persistence.PersistenceException;
import java.util.Map;

/**
 * Action class for handling user management details
 */
public class UserManagementAction extends ActionSupport implements SessionAware, PrincipalAware {

    private static final Logger logger = LoggerFactory.getLogger(UserManagementAction.class);

    private UserManagmentService userManagmentService;

    public UserManagementAction(UserManagmentService userManagmentService) {
        this.userManagmentService = userManagmentService;
    }

    private Map session;

    public String sampleCreateUser() {
        logger.debug("creating a new user  ");
        String ret = ActionSupport.SUCCESS;

        try {
            userManagmentService.createUser(populateUserObject());
        } catch (SriTelRuntimeException e) {
            logger.error("Exception while create user : {} ", e.getMessage());
            ret = ActionSupport.ERROR;
        } catch (PersistenceException e) {
            logger.error("Exception while create user : {} ", e.getMessage());
            ret = ActionSupport.ERROR;
        } catch (Exception e) {
            logger.error("Exception while create user : {} ", e.getMessage());
            ret = ActionSupport.ERROR;
        }
        return ret;
    }

    private User populateUserObject() {
        User user = new User();
        return user;
    }

    @Override
    public void setPrincipalProxy(PrincipalProxy principalProxy) {
    }

    @Override
    public void setSession(Map map) {
        this.session = map;
    }
}
